// const express = require('express')
// const fs = require('fs')
// const path = require('path')
// const ejs = require('ejs')
// //字进程
// const childProcess = require('child_process')
// const app = express()
// app.use(express.static(process.cwd()))

// const port = process.argv[2] ? Number(process.argv[2].split('=')[1]) : 5000

// app.use((req,res,next)=>{
//     console.log(req.path)
//     if(fs.existsSync(process.cwd())){
//         let absPath = path.join(process.cwd(), decodeURIComponent(req.path))
//         console.log(123, absPath)
//         if(!absPath.includes('favicon.ico')) {
//             let dirList = fs.readdirSync(absPath)
//             // 服务器端渲染页面=>前后端不分离
//             let htmlView = fs.readFileSync(path.join(__dirname, 'pages.html'), 'utf-8')
//             res.send(ejs.render(htmlView, {dirList}))
//           } else { 
//               console.log('暂无图标')
//           } 
//     }
    
// })
// app.listen(port,()=>{
//     childProcess.exec(`start http://localhost:${port}`)
//     console.log(`服务器http://localhost:${port}上`)
// })

const express = require('express')
const childProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const ejs = require('ejs')
//实例化
const app = express()
app.use(express.static(process.cwd()))
const port = process.argv[2]?Number(process.argv[2].split('=')[1]):4000
app.use((req,res,next)=>{
    if(fs.existsSync(process.cwd())){
        let absPath = path.join(process.cwd(),decodeURIComponent(req.path))
        if(!absPath.includes('favicon.ico')){
            let dirList  =  fs.readdirSync(absPath)
            let html = fs.readFileSync(path.join(__dirname,'pages.html'),'utf-8')
        
            res.send(ejs.render(html,{dirList}))
        }else{
            console.log('暂无图标')
        }
        
        
    }
})
app.listen(port,()=>{
    childProcess.exec(`start http://localhost:${port}`)
    console.log(`服务器的端口号是${port}`)
})